package com.ruoyi.project.datum.manage.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.datum.collect.service.ICollectService;
import com.ruoyi.project.datum.manage.domain.Manage;
import com.ruoyi.project.datum.manage.service.IManageService;
import com.ruoyi.project.datum.manage.service.IPictureService;
import com.ruoyi.project.datum.series.domain.Series;
import com.ruoyi.project.datum.series.service.ISeriesService;
import com.ruoyi.project.datum.specification.service.ISpecificationService;
import com.ruoyi.project.datum.style.service.IStyleService;
import com.ruoyi.project.datum.view.domain.Photo;
import com.ruoyi.project.datum.view.service.IViewService;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 描述：产品信息 控制层
 * 时间：2020/5/19
 * 作者：WangHaiDong
 */
@Controller
@RequestMapping("/datum/manage")
public class ManageController extends BaseController {

    private String prefix = "datum/manage";

    @Autowired
    private IManageService manageService;

    @Autowired
    private ISeriesService seriesService;

    @Autowired
    private IStyleService styleService;

    @Autowired
    private ISpecificationService specificationService;

    @Autowired
    private ICollectService collectService;

    @Autowired
    private IPictureService pictureService;

    @Autowired
    private IViewService viewService;

    /**
     * 产品信息列表界面
     * @return
     */
    @GetMapping()
    public String datum()
    {
        return  prefix+ "/manage";
    }

    /**
     * 条件查询所有的产品信息
     * @param manage
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Manage manage){
        // BaseController 提供的分页
        startPage();
         List<Manage> list = manageService.selectAll(manage);
        return getDataTable(list);
    }

    /**
     * 新增产品信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("series", seriesService.selectAll(null));
        mmap.put("style",styleService.selectAll(null));
        mmap.put("specification",specificationService.selectAll(null));
        return prefix + "/add";
    }

    /**
     * 上传图片界面
     */
    @GetMapping("/managePhotoView/{manageName}")
    public String edit(@PathVariable("manageName") String manageName, ModelMap mmap)
    {
        //mmap.put("series", seriesService.selectSeriesById(seriesId));
        return prefix + "/managePhotoView";
    }

    /**
     * 根据产品型号 删除产品信息
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(manageService.deleteManageByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 修改产品信息
     */
    @GetMapping("/edit/{manageId}")
    public String edit(@PathVariable("manageId") Long manageId, ModelMap mmap)
    {
        mmap.put("series", seriesService.selectAll(null));
        mmap.put("style",styleService.selectAll(null));
        mmap.put("specification",specificationService.selectAll(null));
        mmap.put("picture",pictureService.selectAll(null));
        mmap.put("manage", collectService.selectManageById(manageId));
        return prefix + "/edit";
    }

    /**
     * 修改保存 产品信息
     * @param manage
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Manage manage)
    {
        manage.setManageCollect(0);
        return toAjax(manageService.updateManage(manage));
    }

    /**
     * 查看产品信息
     */
    @GetMapping("/detail/{manageId}")
    public String detail(@PathVariable("manageId") Long manageId, ModelMap mmap)
    {
        mmap.put("series", seriesService.selectAll(null));
        mmap.put("style",styleService.selectAll(null));
        mmap.put("specification",specificationService.selectAll(null));
        mmap.put("manage", collectService.selectManageById(manageId));

        return prefix + "/detail";
    }


    /**
     * 查看图片库
     */
    @GetMapping("/authPhoto/{priMesId}")
    public String authPhoto(@PathVariable("priMesId") Long priMesId,ModelMap mmap)
    {
        mmap.put("priMesId",priMesId);
        return prefix + "/authPhoto";
    }

    /**
     * 查询所有的图片列表
     */
    @PostMapping("/authPhoto/photoList")
    @ResponseBody
    public TableDataInfo allocatedList(Photo photo)
    {
        startPage();
        List<Photo> list = viewService.selectPhotoList(photo);
        return getDataTable(list);
    }

    /**
     * 修改图片显示
     * @param photo
     * @return
     */
    @PostMapping("/authPhoto/changePhoGalShow")
    @ResponseBody
    public AjaxResult changePhoGalShow(Photo photo)
    {
        return toAjax(viewService.changePhoGalShow(photo));
    }


    /**
     * 修改产品信息
     */
    @GetMapping("/authPhoto/edit/{phoGalId}")
    public String editPhoto(@PathVariable("phoGalId") Long phoGalId, ModelMap mmap)
    {
        mmap.put("photo", viewService.selectPhotoById(phoGalId));
        return prefix + "/editPhoto";
    }

    /**
     * 修改保存 产品信息
     * @param photo
     * @return
     */
    @PostMapping("/editPhoto")
    @ResponseBody
    public AjaxResult editPhotoSave(@Validated Photo photo)
    {
        return toAjax(viewService.updatePhoto(photo));
    }

}
